<template>
  <div>
    <div class="search">
      <span>人员搜索：</span><el-input v-model="userName" style="width: 260px" placeholder="请输入" />
      <el-button icon="el-icon-search" @click="search">查询</el-button>
    </div>
    <div class="body">
      <el-button icon="el-icon-circle-plus-outline" class="add" @click="add">新建</el-button>
      <!-- table表格 -->
      <el-table :data="userList" style="width: 100%">
        <el-table-column
          type="index"
          :index="indexMethod"
          label="序号"
          width="80"
        />
        <el-table-column prop="userName" label="人员名称" width="300" />
        <el-table-column prop="regionName" label="归属地区" width="260" />
        <el-table-column prop="role.roleName" label="角色" width="260" />
        <el-table-column prop="mobile" label="联系电话" width="260" />
        <el-table-column label="操作" width="180">
          <template v-slot="{row}">
            <el-button
              size="mini"
              type="text"
              style="color: blue"
              @click="chooseUser(row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              style="color: red"
              @click="delUser(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div ref="page" class="page">
        <span
          class="sum"
        >共{{ totalCount }}条记录 第 {{ pageIndex }}/{{ totalPage }} 页</span>
        <el-pagination
          small
          layout="prev, pager, next"
          prev-text="上一页"
          next-text="下一页"
          :total="+totalCount"
          @prev-click="prevclick"
          @next-click="nextclick"
        />
      </div>
    </div>
    <!-- 新建&修改弹层 -->
    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
      :close-on-click-modal="false"
      @close="close"
    >
      <template #title><span class="centerDialogVisibleTitle">{{ title }}</span></template>
      <el-form ref="formLabelAlign" label-width="120px" :model="formLabelAlign">
        <el-form-item prop="userName" label="人员名称:">
          <el-input v-model="formLabelAlign.userName" show-word-limit maxlength="5" placeholder="请输入" style="width: 80%" />
        </el-form-item>
        <el-form-item prop="roleName" label="角色：">
          <el-select v-model="formLabelAlign.roleName" style="width:80%" placeholder="请选择">
            <el-option
              v-for="item in userTypeList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleName
              "
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="mobile" label="联系电话：">
          <el-input v-model="formLabelAlign.mobile" placeholder="请输入" show-word-limit maxlength="11" style="width: 80%" />
        </el-form-item>
        <el-form-item label="负责区域：" prop="regionName">
          <el-select v-model="formLabelAlign.regionName" style="width:80%" placeholder="请选择">
            <el-option
              v-for="item in regionTypeList"
              :key="item.id"
              :label="item.remark"
              :value="item.remark"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="image" label="头像:">
          <image-upload v-model="formLabelAlign.image" />
          <span>支持扩展名：jpg、png，文件不得大于100kb</span>
        </el-form-item>
        <el-form-item prop="status" label="状态:">
          <el-checkbox v-model="formLabelAlign.status">是否启用</el-checkbox>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          class="esc"
          @click="close"
        >取 消</el-button>
        <el-button
          class="enter"
          type="primary"
          @click="addEnter"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ImageUpload from '@/views/user/components/image-upload.vue'
import { delUser, chooseUser, getUserList, addUser, getUserType, getRegionType } from '@/api/userPage'
export default {
  name: 'Index',
  components: {
    ImageUpload
  },
  data() {
    return {
      userTypeList: [],
      regionTypeList: [],
      formLabelAlign: {
        // 人员名称
        userName: '',
        // 角色
        roleId: null, // 角色ID
        roleName: '',
        // 联系电话
        mobile: '',
        // 负责区域
        regionId: '', // 所属区域ID
        regionName: '',
        // 头像
        image: '', // 头像Url
        // 状态
        status: false
      },
      // 新增修改弹层
      title: '',
      centerDialogVisible: false,
      checked: false,
      // 搜索
      type: 'getUserList',
      userName: '',
      pageIndex: 1,
      pageSize: 10,
      totalCount: null,
      totalPage: null,
      userList: []
    }
  },
  created() {
    if (this.type === 'getUserList') {
      this.getUserList()
    }
    this.getUserType()
    this.getRegionType()
  },
  methods: {
    // 删除
    async delUser(id) {
      await delUser(id)
      this.$message.success({
        message: '删除人员成功',
        offset: '90'

      })
      this.getUserList()
    },
    // 修改
    chooseUser(row) {
      console.log(row)
      this.title = '修改人员'
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.formLabelAlign = { ...row, ...row.role }
      })
    },
    // 关弹窗
    async close() {
      await this.$refs.formLabelAlign.resetFields()
      this.centerDialogVisible = false
    },
    // 确认新建&修改
    async addEnter() {
      if (this.title === '新增人员') {
        this.formLabelAlign.roleId = this.userTypeList.filter(item => item.roleName === this.formLabelAlign.roleName)[0].roleId
        this.formLabelAlign.regionId = this.regionTypeList.filter(item => item.remark === this.formLabelAlign.regionName)[0].id
        try {
          await addUser({ ...this.formLabelAlign })
          this.$message.success({
            message: '新增人员成功',
            offset: 90
          })
          this.close()
        } catch (error) {
          this.$message.warning({
            message: error.response.data,
            offset: 90
          })
        }
      } else if (this.title === '修改人员') {
        this.formLabelAlign.roleId = this.userTypeList.filter(item => item.roleName === this.formLabelAlign.roleName)[0].roleId
        this.formLabelAlign.regionId = this.regionTypeList.filter(item => item.remark === this.formLabelAlign.regionName)[0].id
        try {
          await chooseUser({ ...this.formLabelAlign })
          this.$message.success({
            message: '修改人员成功',
            offset: 90
          })
          this.close()
        } catch (error) {
          this.$message.warning({
            message: error.response.data,
            offset: 90
          })
        }
      }
      this.getUserList()
    },
    // 新建
    add() {
      this.title = '新增人员'
      this.centerDialogVisible = true
    },
    // 获取角色列表
    async getUserType() {
      const res = await getUserType()
      this.userTypeList = res
    },
    // 获取区域列表
    async getRegionType() {
      const res = await getRegionType()
      this.regionTypeList = res.currentPageRecords
    },
    // 搜索
    async search() {
      this.type = 'search'
      const res = await getUserList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        userName: this.userName
      })
      this.userList = res.currentPageRecords
      if (res.currentPageRecords.length <= 1 && this.pageIndex === 1) {
        this.$refs.page.style.display = 'none'
      }
      this.totalCount = res.totalCount
      this.totalPage = res.totalPage
    },
    // 切换分页
    prevclick() {
      this.pageIndex--
      this.type === 'getUserList' ? this.getUserList() : this.search()
    },
    nextclick() {
      if (this.pageIndex < this.totalPage) {
        this.pageIndex++
        this.type === 'getUserList' ? this.getUserList() : this.search()
      }
    },
    // 获取列表
    async getUserList() {
      const res = await getUserList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      })
      this.userList = res.currentPageRecords
      if (this.userList.length <= 1 && this.pageIndex === 1) {
        this.$refs.page.style.display = 'none'
      }
      this.totalCount = res.totalCount
      this.totalPage = res.totalPage
    },
    // 序号
    indexMethod(index) {
      return index + 1 + (this.pageIndex - 1) * this.pageSize
    }
  }
}
</script>

<style scoped lang="scss">
//新增修改弹层
::v-deep .el-dialog {
  border-radius: 10px;
  .centerDialogVisibleTitle{
    float: left;
  }
  .dialog-footer {
    .esc {
      background-color: #fbf4f0 !important;
      border: none;
      color: #655b56 !important;
    }
    .enter {
      color: #fff;
      background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
      border: 0;
    }
  }
}
//搜索
.search {
  background-color: #fff;
  height: 60px;
  line-height: 60px;
  padding-left: 15px;
  margin-right: 20px;
  .el-input {
    padding: 0 15px;
  }
  .el-button {
    background-color: #5f84ff;
    color: #fff;
  }
}
//table表格
.body {
  background-color: #fff;
  margin-right: 20px;
  margin-top: 20px;
  .add {
    color: #fff;
    background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
    border: 0;
    margin: 20px 0 20px 15px;
  }
  .el-table {
    padding-left: 15px;
    .el-table-column {
      height: 20px;
    }
  }
  .page {
    display: flex;
    align-items: center;
    padding: 30px 30px 50px 20px;
    .sum {
      color: #dbdfe5 !important;
    }
    //分页器
    .el-pagination {
      margin-left: 980px;
      ::v-deep .el-pager {
        display: none;
      }
      ::v-deep .btn-next {
        margin-left: 20px;
        border-radius: 3px;
        width: 80px;
        height: 35px;
        text-align: center;
        background-color: #d5ddf8;
        cursor: default;
      }
      ::v-deep .btn-prev {
        border-radius: 3px;
        width: 80px;
        height: 35px;
        text-align: center;
        background-color: #d5ddf8;
        cursor: default;
      }
      ::v-deep .btn-next :disabled {
        color: #c0c4cc;
      }
      ::v-deep .btn-prev :disabled {
        color: #c0c4cc;
      }
    }
  }
}
</style>
